var _Server = require('server.js');
module.exports = {
    requestUrl: function(data, url, callback, pageobj, showloading, methods) {
        if (showloading) {
            wx.showLoading({
                title: '请稍后...',
            });
        }
        var headers = {};
        if (methods == "POST"){
            methods = "POST";
            headers = {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
            
        } else {
            methods = "GET";
            headers = {
                'Content-Type': 'application/json'
            }
        }
        wx.request({
            url: url,
            data: data,
            method: methods,
            header: headers,
            success: function(res) {
                if (showloading) {
                    wx.hideLoading();
                }
                if (res.statusCode == 200) {
                    callback.apply(pageobj, [res.data])
                } else {
                    var error_msg = 'error:接口请求错误';
                    if (res.data.msg != 'null' || res.data.msg != '') {
                        error_msg = res.data.msg
                    }
                    wx.showModal({
                        title: '提示',
                        content: error_msg,
                        showCancel: false
                    })
                }
            },
            complete: function() {
                if (showloading) {
                    wx.hideLoading();
                }
            }
        })
    },
    getConfig: function (callback, pageobj) { // 系统配置/电话
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.getConfig, callback, pageobj);
    },
    getIndexData: function(callback, pageobj) { // 首页数据请求
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.index_url, callback, pageobj, true);
    },
    getLoginToken: function (code, admin_id, callback, pageobj) { // 获取token
        var data = {
            code: code,
            admin_id: admin_id,
            _: Date.now()
        };
        this.requestUrl(data, _Server.login_url, callback, pageobj);
    },
    saveUserInfo: function (nickname, avatar, callback, pageobj){ // 保存用户信息
        var data = {
            nickname: nickname,
            avatar: avatar,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        console.log(data)
        this.requestUrl(data, _Server.saveUserInfo, callback, pageobj, true, "POST");
    },
    getUserInfo: function (callback, pageobj) { // 获取用户信息
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.userInfo_url, callback, pageobj);
    },
    getCourseDetail: function (course_id, callback, pageobj){ // 课程详情
        var data = {
            course_id: course_id,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        console.log(data)
        this.requestUrl(data, _Server.courseDetail, callback, pageobj);
    },
    getHotWord: function (callback, pageobj) { // 首页-热门关键词
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.getHotWord, callback, pageobj);
    },
    courseList: function (category_id, search, callback, pageobj) { // 首页-往日精彩-课程列表
        var data = {
            category_id: category_id,
            search: search,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.courseList, callback, pageobj);
    },
    courseInfo: function (article_id, callback, pageobj) { // 首页-往日精彩-课程详情
        var data = {
            article_id: article_id,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.courseInfo, callback, pageobj);
    },
    homeStudentList: function (page, callback, pageobj) { // 首页-大咖学员列表
        var data = {
            page: page,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.homeStudentList, callback, pageobj, true);
    },
    homeStudentInfo: function (student_id, callback, pageobj) { // 首页-大咖学员详情
        var data = {
            student_id: student_id,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.homeStudentInfo, callback, pageobj);
    },
    getBugCourseInfo: function (dataObj, callback, pageobj){ // 提交课程报名信息。返回订单ID
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        data = Object.assign(data, dataObj);
        console.log(data);
        this.requestUrl(data, _Server.buyCourseInfo, callback, pageobj, true, "POST");
    },
    getWxPay: function (orderId, num, callback, pageobj) { // 获取支付参数
        var data = {
            order_id: orderId,
            num: num,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        console.log(data);
        this.requestUrl(data, _Server.wxPay_url, callback, pageobj, true);
    },
    orderCourseList: function (page, callback, pageobj) { // 我的-订单课程列表
        var data = {
            page: page,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.orderCourseList_url, callback, pageobj, true);
    },
    orderCourseDetails: function (orderId, callback, pageobj) { // 我的-订单课程详情
        var data = {
            order_id: orderId,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.orderCourseDetails_url, callback, pageobj, true);
    },
    myProductDetails: function (productId, callback, pageobj) { // 我的-套餐详情
        var data = {
            product_id: productId,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.myProductDetails, callback, pageobj, true);
    },
    myProductOrderDetails: function (orderId, callback, pageobj) { // 我的-套餐订单详情
        var data = {
            order_id: orderId,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        console.log(data);
        this.requestUrl(data, _Server.myProductOrderDetails, callback, pageobj, true);
    },
    myProductPayOrder: function (courseId,productId, callback, pageobj) { // 我的-套餐订单支付信息ID
        var data = {
            course_id: courseId,
            product_id: productId,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.myProductPayOrder, callback, pageobj, true,'POST');
    },
    myProductPay: function (orderId, type, callback, pageobj) { // 我的-套餐订单支付签名
        var data = {
            order_id: orderId,
            type: type,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        console.log(data);
        this.requestUrl(data, _Server.myProductPay, callback, pageobj, true);
    },
    myProductList: function (status, type, page, callback, pageobj) { // 我的-套餐列表
        var data = {
            status:status,
            type:type,
            page: page,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.myProductList, callback, pageobj, true);
    },
    homeCompanyInfo: function(callback, pageobj) { // 首页-公司介绍
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.homeCompanyInfo, callback, pageobj, true);
    },
    homeCaseList: function (page,callback, pageobj) { // 首页-实战案例列表
        var data = {
            page: page,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.homeCaseList, callback, pageobj, true);
    },
    homeCaseInfo: function (case_id,callback, pageobj) { // 首页-实战案例详情
        var data = {
            case_id: case_id,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.homeCaseInfo, callback, pageobj, true);
    },
    myMedal: function (callback, pageobj) { // 首页-荣誉奖章
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.myMedal, callback, pageobj, true);
    },
    myShare: function (callback, pageobj) { // 首页-邀请函
        var data = {
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.myShare, callback, pageobj, true);
    },
    myUpimg: function (image, callback, pageobj) { // 首页-上传图片
        var data = {
            image: image,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.myUpimg, callback, pageobj, true,'POST');
    },
    myProblem: function (content, images, phone, callback, pageobj) { // 首页-意见反馈
        var data = {
            content: content,
            images: images,
            phone: phone,
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.myProblem, callback, pageobj, true, 'POST');
    },
    teacherList: function (type, age, sex, callback, pageobj) { // 专家团队-列表
        var data = {
            type: type, // 1：讲师；2：专家
            age: age, // 年龄段
            sex: sex, // 性别
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.teacherList, callback, pageobj, true);
    },
    teacherInfo: function (teacher_id, callback, pageobj) { // 转接团队-详情
        var data = {
            teacher_id: teacher_id, // 讲师ID
            token: wx.getStorageSync('token'),
            _: Date.now()
        };
        this.requestUrl(data, _Server.teacherInfo, callback, pageobj, true);
    },

};